package com.ma.contacts;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;

import com.ma.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import base.BaseActivity;
import lomasky.xui.BaseViewHolder;
import lomasky.xui.ItemDivider;
import lomasky.xui.RecyclerViewAdapter;
import tellh.com.recyclertreeview_lib.TreeNode;
import tellh.com.recyclertreeview_lib.TreeViewAdapter;

public class ChooseContactsActivity extends BaseActivity {
    private RecyclerView rv, contactsRecyclerView;
    private TreeViewAdapter adapter;
    private RecyclerViewAdapter<Emp> contactsRecyclerViewAdpter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choose_contacts);
        initView();
        initData();
    }

    private void initData() {
        List<TreeNode> nodes = new ArrayList<>();


        {
            TreeNode<Dir> app = new TreeNode<>(new Dir("集团公司"));
            nodes.add(app);
            app.addChild(
                    new TreeNode<>(new Dir("分公司")).addChild(
                            new TreeNode<>(new Dir("部门")).addChild(
                                    new TreeNode<>(new Dir("大组")).addChild(new TreeNode(new File("小组")))))
            );
        }

        rv.setLayoutManager(new LinearLayoutManager(this));
        rv.addItemDecoration(new ItemDivider(this));
        adapter = new TreeViewAdapter(nodes, Arrays.asList(new FileNodeBinder(), new DirectoryNodeBinder()));

        adapter.setOnTreeNodeListener(new TreeViewAdapter.OnTreeNodeListener() {
            @Override
            public boolean onClick(TreeNode node, RecyclerView.ViewHolder holder) {
                if (!node.isLeaf()) {
                    //Update and toggle the node.
                    onToggle(!node.isExpand(), holder);
//                    if (!node.isExpand())
//                        adapter.collapseBrotherNode(node);
                }
                return false;
            }

            @Override
            public void onToggle(boolean isExpand, RecyclerView.ViewHolder holder) {
                DirectoryNodeBinder.ViewHolder dirViewHolder = (DirectoryNodeBinder.ViewHolder) holder;
                final ImageView ivArrow = dirViewHolder.getIvArrow();
                int rotateDegree = isExpand ? 90 : -90;
                ivArrow.animate().rotationBy(rotateDegree)
                        .start();
            }
        });
        rv.setAdapter(adapter);
    }

    private void initView() {
        rv = (RecyclerView) findViewById(R.id.rv);
        contactsRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);

        GridLayoutManager layoutManage = new GridLayoutManager(this, 2);
        contactsRecyclerView.setLayoutManager(layoutManage);

        contactsRecyclerViewAdpter = new RecyclerViewAdapter<Emp>(this, R.layout.contacts_radio_item) {
            @Override
            public void onBind(BaseViewHolder holder, Emp item, int position) {

                holder.getRiadioButton(R.id.radio).setText(item.getName());
                holder.getRiadioButton(R.id.radio).setChecked(item.isCheck());

            }


        };
        List<Emp> emps = new ArrayList<>();

        emps.add(new Emp("雇员1", false));
        emps.add(new Emp("雇员2", false));
        emps.add(new Emp("雇员3", false));
        emps.add(new Emp("雇员4", false));
        contactsRecyclerViewAdpter.setData(emps);


        contactsRecyclerViewAdpter.setOnItemClickListener((view, position) -> {
            for (int i = 0; i < emps.size(); i++) {
                emps.get(i).setCheck(i == position);
            }
            contactsRecyclerViewAdpter.notifyDataSetChanged();


        });
        contactsRecyclerView.setAdapter(contactsRecyclerViewAdpter);

    }


}
